package com.aisi.manager.service.impl;

import com.aisi.commonService.exception.AiSiException;
import com.aisi.commonUtil.AuthContextUtil;
import com.aisi.manager.mapper.SysMenuMapper;
import com.aisi.manager.mapper.SysRoleMenuMapper;
import com.aisi.manager.service.SysMenuService;
import com.aisi.manager.utils.MenuHelper;
import com.aisi.model.entity.system.SysMenu;
import com.aisi.model.entity.system.SysUser;
import com.aisi.model.vo.common.ResultCodeEnum;
import com.aisi.model.vo.system.SysMenuVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: shenjianZ
 * @Date: 2024/2/18 9:08
 * @Description:
 */

@Service
@Slf4j
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findAllMenus() {
        List<SysMenu> sysMenus = sysMenuMapper.findAllMenus();
        return MenuHelper.buildMenuTree(sysMenus);
    }

    @Override
    public void saveSysMenu(SysMenu sysMenu) {
        sysMenuMapper.saveSysMenu(sysMenu);
        updateParentMenuIsHuf(sysMenu); // 更新父菜单的isHuf字段，递归更新
    }

    private void updateParentMenuIsHuf(SysMenu sysMenu) {
        SysMenu sysParentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());
        if (sysParentMenu != null) {
            sysRoleMenuMapper.updateParentMenuIsHuf(sysParentMenu.getId());
        }
    }

    @Override
    public void updateSysMenuById(SysMenu sysMenu) {
        sysMenuMapper.updateSysMenuById(sysMenu);
    }

    @Override
    public void deleteSysMenuById(Long id) {
        int count = sysMenuMapper.countByParentId(id);
        if (count > 0)
            throw new AiSiException(ResultCodeEnum.NODE_ERROR);
        sysMenuMapper.deleteSysMenuById(id);
    }

    @Override
    public List<SysMenuVo> findUserMenuList() {
        SysUser sysUser = AuthContextUtil.getAuthContext();
        Long userId = sysUser.getId();
        log.info("用户id:{}", userId);
        List<SysMenu> sysMenus = sysMenuMapper.selectListByUserId(userId);
        //构建树形数据
        List<SysMenu> sysMenuTreeList = MenuHelper.buildMenuTree(sysMenus);
        return this.buildMenus(sysMenuTreeList);
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<SysMenuVo>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
